<template>
  <div class="phone-wrap">
    <div class="phone-box">
      <div class="wrap-box">
        <div class="icon1"><img src="../../images/phone.png" class="phone1"/></div>
        <div class="input-box"><input type="text" class="phone2" placeholder="请输入手机号码" maxlength="11" v-model="phone"
                                      v-on:input="checkIcon" @blur="inputBlur"/></div>
        <div class="icon2" v-if="showIcon"><img src="../../images/icon_1.png" class="phone3"/></div>
        <div class="icon2" v-if="hideIcon"><img src="../../images/icon_2.png" class="phone3"/></div>
      </div>
      <div class="wrap-box">
        <div class="icon3"><img src="../../images/code.png" class="pwd1"/></div>
        <div class="code-box"><input type="text" class="code" placeholder="请输入验证码" maxlength="6" v-model="codeNum"
                                     @blur="inputBlur"/>
        </div>
        <div class="icon5">
          <timer-btn ref="timerBtn" class="btn" v-on:run="sendCode"></timer-btn>
        </div>
      </div>

      <div class="tip-box" v-if="type == 1">当前手机号已丢失，回答密保问题修改绑定&nbsp;>></div>
      <div class="tip-box" v-if="type == 2" @click="call">当前手机号已丢失，请联系客服&nbsp;>></div>

    </div>
    <div class="btn-box">
      <button type="button" class="btn" @click="confirm">确定</button>
    </div>
  </div>
</template>

<script>
  import code from '../../components/code/code'
  import {baseUrl} from "../../common/js/env"

  export default {
    data() {
      return {
        showIcon: false,
        hideIcon: false,
        phone: "",
        codeNum: "",
        token1: "",
        token2: "",
        userSign: "",
        type: 0,
        problem: "",
      }
    },
    mounted() {
      this.problem = sessionStorage.getItem("problem");
      if (this.problem == 0) {
        this.type = 2;
      } else {
        this.type = 1;
      }
      let base64 = require('js-base64').Base64;
      this.userSign = base64.decode(sessionStorage.getItem("user_sign"));
    },
    methods: {
      checkIcon() {
        let phone = this.phone;
        let reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
        if (!reg.test(phone.trim())) {
          this.showIcon = false;
          this.hideIcon = true;
        } else {
          this.showIcon = true;
          this.hideIcon = false;
        }
      },
      checkPhone() {//校验手机号码
        let phone = this.phone;
        let reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/; //手机号码正则表达式
        if (phone == "") {
          this.$vux.toast.text("手机号码不能为空", "middle");
          return;
        }
        if (!reg.test(phone)) {
          this.$vux.toast.text("手机号码格式错误", "middle");
          return;
        }
        return true;
      },
      checkCode() {
        let reg = /^[0-9]*$/; //验证数字
        let code = this.codeNum;
        if (code == "") {
          this.$vux.toast.text("验证码不能为空", "middle");
          return;
        } else if (!reg.test(code)) {
          this.$vux.toast.text("验证码格式不正确", "middle");
          return;
        }
        return true;
      },
      sendCode() {//获取验证码
        let phone = this.phone;
        if (this.checkPhone()) {
          this.$refs.timerBtn.setDisabled(true);//设置按钮不可用
          this.axios.get(baseUrl + "/login/get_token?phone=" + phone + "&i=2").then((res) => {
            let resultObj = res.data;
            if (resultObj.code == 0 && resultObj.data != null) {
              this.token1 = resultObj.data;
              this.axios.get(baseUrl + "/login/send_code?phone=" + phone + "&i=2" + "&userToken=" + this.token1).then((res) => {
                let resultObj = res.data;
                if (resultObj.code == 0) {
                  this.token2 = resultObj.data;
                  this.$refs.timerBtn.start(); //启动倒计时
                } else {
                  this.$vux.toast.text(resultObj.msg, "middle");
                  this.$refs.timerBtn.stop();
                }
              }).catch(function (error) {
                this.$vux.toast.text("网络异常", "middle");
              });
            } else {
              this.$vux.toast.text(resultObj.msg, "middle");
            }
          }).catch(function (error) {
            this.$vux.toast.text("网络异常", "middle");
          });
        }
      },
      confirm() {//确定
        let phone = this.phone;
        let code = this.codeNum;
        if (this.checkPhone() && this.checkCode()) {
          //验证旧手机号
          this.axios.post(baseUrl + "/userMaterial/get_tel_old?sgin=" + this.userSign + "&oldUserTel=" + phone + "&code=" + code + "&token=" + this.token2 + "&i=2").then((res) => {
            let resultObj = res.data;
            this.$vux.loading.hide();
            if (resultObj.code == 0 && resultObj.data != null) {
              if (resultObj.data == 0) {
                this.$vux.toast.text("手机号验证不通过", "middle");
              } else {
                if (this.problem == 0) {
                  this.$router.push("/secret");//设置密保
                } else {

                }
              }
            } else {
              this.$vux.toast.text(resultObj.msg, "middle");
              return;
            }
          }).catch(() => {
            this.$vux.loading.hide();
            this.$vux.toast.text("网络异常", "middle");
          });
        }
      },
      secret() {

      },
      inputBlur() {
        window.scrollTo(0, 0);
      },
      call() {//拨打客服
        this.axios.get(baseUrl + "/customerser/get_customer_tel?sgin=" + this.userSign).then((res) => {
          let resultObj = res.data;
          if (resultObj.code == 0 && resultObj.data != null) {
            window.location.href = 'tel://' + resultObj.data;
          } else {
            this.$vux.toast.text(mateObj.msg, "middle");
          }
        }).catch(() => {
          this.$vux.toast.text("网络异常", "middle");
        });
      }
    },
    components: {
      "timer-btn": code
    }
  }
</script>

<style lang="stylus" scoped>
  @import "../../common/stylus/mixin.styl"

  .phone-wrap
    wh(100%, 100%)
    overflow: hidden
    .phone-box
      width: 100%
      margin-top: 0.8rem
      .wrap-box
        display: flex
        align-items: center
        margin: 0 auto
        wh(92.53%, 1.133rem)
        border-bottom: 1px solid rgba(230, 230, 230, 1)
        margin-bottom: 0.547rem
        .icon1
          width: 0.787rem
          .phone1
            wh(0.413rem, 0.493rem)
        .icon3
          width: 0.787rem
          .pwd1
            wh(0.413rem, 0.493rem)
        .input-box
          width: auto
          .phone2
            wh(100%, 100%)
            size-color(0.4rem, rgba(169, 169, 169, 1))
        .icon2
          wh(0.533rem, 0.533rem)
          margin-left: auto
          .phone3
            wh(100%, 100%)
        .code-box
          width: auto
          .code
            wh(100%, 100%)
            size-color(0.4rem, rgba(169, 169, 169, 1))
        .icon5
          wh(auto, 0.4rem)
          margin-left: auto
          font-size: 0.4rem
          .btn
            line-height: 0.4rem
            text-align: center
            color: rgba(60, 175, 255, 1)
            background: #FFF
      .tip-box
        position: fixed
        bottom: 2.907rem
        text-align: center
        wh(100%, auto)
        size-color(0.347rem, rgba(60, 175, 255, 1))
    .btn-box
      fj(center, center)
      position: fixed
      bottom: 0.48rem
      wh(100%, 1.333rem)
      border-radius(0.107rem)
      .btn
        wh(92.53%, 1.333rem)
        background: rgba(60, 175, 255, 1)
        border-radius(0.107rem)
        size-color(0.48rem, rgba(255, 255, 255, 1))
</style>
